package com.neulogistics.core.substation.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blackto.lib.mediat.core.IMediator;
import com.neulogistics.core.substation.api.application.command.AssignCourierCommand;
import com.neulogistics.core.substation.api.application.command.CreateTaskOrderCommand;
import com.neulogistics.core.substation.api.application.command.ReceiptCommand;
import com.neulogistics.core.substation.api.application.query.ITaskOrderQueryService;
import com.neulogistics.core.substation.api.application.query.SubstationQuery;
import com.neulogistics.core.substation.api.application.query.TaskOrderQuery;
import com.neulogistics.core.substation.api.application.viewmodel.SubstationViewModel;
import com.neulogistics.core.substation.api.application.viewmodel.TaskOrderViewModel;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

@RestController
@Api(tags = "任务单管理")
@RequestMapping("/substation/task")
public class TaskOrderController {
    @Resource
    private IMediator mediator;

    @Resource
    ITaskOrderQueryService taskOrderQueryService;

    @ApiOperation("创建任务单")
    @PostMapping("/create")
    public BaseResponse<String> createCustomer(@RequestBody @Validated @NotNull CreateTaskOrderCommand command){
        // 已校验数据格式
        BaseResponse<String> baseResponse = mediator.send(command);
        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("分配配送员")
    @PostMapping("/assign")
    public BaseResponse<Boolean> assign(@RequestBody @Validated @NotNull AssignCourierCommand command){
        // 已校验数据格式

        BaseResponse<Boolean> baseResponse = mediator.send(command);
        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("填写回执")
    @PostMapping("/receipt")
    public BaseResponse receipt(@RequestBody @Validated @NotNull ReceiptCommand command){
        // 已校验数据格式

        BaseResponse baseResponse = mediator.send(command);
        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("根据id查询任务单")
    @PostMapping("/get/{id}")
    public BaseResponse<TaskOrderViewModel> get(@PathVariable(value = "id") @NotNull @Positive Long id){
        // 已校验数据格式

        TaskOrderViewModel taskOrderViewModel = taskOrderQueryService.getById(id);
        return ResultUtils.success(taskOrderViewModel);
    }

    @ApiOperation("分页查询任务单")
    @PostMapping("/page")
    public BaseResponse<Page<TaskOrderViewModel>> page(@RequestBody @NotNull @Validated TaskOrderQuery taskOrderQuery){
        // 已校验数据格式

        Page<TaskOrderViewModel> viewModelPage = taskOrderQueryService.page(taskOrderQuery);
        return ResultUtils.success(viewModelPage);
    }
}
